﻿Imports vb = Microsoft.VisualBasic

Public Class MainForm
    Private STARTSTR As String = "80003200"
    'Private Const STARTSTR As String = "80003200"
    Private FIRSTSTR As String = "0400" & vb.Right(STARTSTR, 4)

    Private Sub btConvert_Click(sender As System.Object, e As System.EventArgs) Handles btConvert.Click
        If String.IsNullOrEmpty(txtGecko.Text.Trim) Then
            MsgBox("请输入内容")
            Exit Sub
        End If

        '判断首地址
        If txtStartstr.Text.Length = 8 Then
            STARTSTR = txtStartstr.Text
            FIRSTSTR = "0400" & vb.Right(STARTSTR, 4)
        Else
            MsgBox("地址不足8位")
            Exit Sub
        End If

        Dim result As New List(Of String)
        Dim gecko = txtGecko.Text.Trim.Replace(" " & vbCrLf, vbCrLf).Replace(" ", vbCrLf).Split(vbCrLf)
        For i = 0 To gecko.Length - 1
            '第一行
            If i = 0 Then
                result.Add("04" & vb.Right(gecko(i), 6))
                Dim temp = Hex(CLng("&h" & STARTSTR) - CLng("&h" & "80" & vb.Right(gecko(i), 6)))
                result.Add("4B" & vb.Right(temp, 6).PadLeft(6, "0"))
                Continue For
            End If
            If i = 1 Then
                Continue For
            End If
            result.Add(Hex(CLng("&h" & FIRSTSTR) + (i - 2) * 4).PadLeft(8, "0"))

            '最后一行
            If i = gecko.Length - 1 Then
                Dim temp = "48" & vb.Right(Hex(CLng("&h80" & vb.Right(gecko(0), 6)) + 4 - CLng("&h80" & vb.Right(Hex(CLng("&h" & FIRSTSTR) + (i - 2) * 4).PadLeft(8, "0"), 6))).PadLeft(6, "0"), 6)
                result.Add(temp)
                Continue For
            End If

            result.Add(gecko(i))
        Next

        '输出
        Dim resultstr = ""
        Dim resultarr = result.ToArray()
        For i = 0 To resultarr.Length - 1 Step 2
            resultstr += resultarr(i).Trim & " " & resultarr(i + 1).Trim & vbCrLf
        Next
        txtAR.Text = resultstr
    End Sub
End Class
